package com.opengate.base.intercept;

import com.opengate.base.constants.AppConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description Feign拦截器
 * @date 2023/4/26 5:46 PM
 */
@Slf4j
@Configuration
public class FeignInterceptor implements RequestInterceptor {

    /**
     * 拦截Feign请求，将请求头中的rid传递到下游服务
     *
     * @param requestTemplate 请求模板
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取请求头中的rid
        String requestId = MDC.get(AppConstants.HEADER_RID_NAME);
        if (StringUtils.isNotBlank(requestId)) {
            //  将rid传递到下游服务
            requestTemplate.header(AppConstants.HEADER_RID_NAME, requestId);
        }
    }
}
